package com.dhcc.sds.standard.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.dhcc.cdc.busi.dao.CdcExamdicMapper;
import com.dhcc.cdc.busi.dao.CdcLabItemMapper;
import com.dhcc.cdc.busi.dao.CdcLabOrderMapper;
import com.dhcc.cdc.busi.dao.CdcOrdmastMapper;
import com.dhcc.cdc.busi.entity.CdcExamdicEntity;
import com.dhcc.cdc.busi.entity.CdcLabItemEntity;
import com.dhcc.cdc.busi.entity.CdcLabOrderEntity;
import com.dhcc.cdc.busi.entity.CdcOrdmastEntity;
import com.dhcc.sds.standard.dao.SdsStandDicMapper;
import com.dhcc.sds.standard.dao.SdsStandDicsubMapper;
import com.dhcc.sds.standard.dao.SdsStandMatchLogMapper;
import com.dhcc.sds.standard.entity.SdsStandDicEntity;
import com.dhcc.sds.standard.entity.SdsStandDicsubEntity;
import com.dhcc.sds.standard.entity.SdsStandMatchLogEntity;
import com.dhcc.sds.standard.service.ISdsStandMatchLogService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


/**
 * 标准化对照日志记录表表数据服务层接口
 * 
 * @ClassName: StMatchLogServiceImpl
 * @Description: TODO
 * @author zhangdc
 * @date 2021-05-08 16:01:23
 */
@Service("stMatchLogService")
public class SdsStandMatchLogServiceImpl extends BaseServiceImpl<SdsStandMatchLogMapper, SdsStandMatchLogEntity> implements ISdsStandMatchLogService {

	@Resource
	private SdsStandDicsubMapper standDicsubMapper;

	@Resource
	private SdsStandDicMapper standDicMapper;

	@Resource
	private CdcOrdmastMapper ordmastMapper;

	@Resource
	private CdcExamdicMapper cdcExamdicMapper;

	@Resource
	private CdcLabOrderMapper cdcLabOrderMapper;

	@Resource
	private CdcLabItemMapper cdcLabitemMapper;

	@Override
	public List<SdsStandMatchLogEntity> page(Page<SdsStandMatchLogEntity> page, Map<String, Object> map) {
		List<SdsStandMatchLogEntity> page1 = this.baseMapper.page(page, map);
		for (SdsStandMatchLogEntity sdsStandMatchLogEntity : page1){
			Long dicId = sdsStandMatchLogEntity.getDicId();
			Long dicsubId = sdsStandMatchLogEntity.getDicsubId();
			String matchType = sdsStandMatchLogEntity.getMatchType();
			Long ordmastId = sdsStandMatchLogEntity.getOrdmastId();
			Long examdicId = sdsStandMatchLogEntity.getExamdicId();
			Integer operType = sdsStandMatchLogEntity.getOperType();
			Long laborderId = sdsStandMatchLogEntity.getLaborderId();
			Long labitemId = sdsStandMatchLogEntity.getLabitemId();
			String operTypeDesc;
			if (operType == 1){
				operTypeDesc = "对照";
			}else{
				operTypeDesc = "撤销";
			}

			if (!StringUtils.isEmpty(dicId)){
				SdsStandDicEntity standDicEntity = standDicMapper.selectById(dicId);
				if (!StringUtils.isEmpty(standDicEntity)){
					sdsStandMatchLogEntity.setDicDesc(standDicEntity.getName());
				}
			}else {
				SdsStandDicsubEntity dicsubEntity = standDicsubMapper.selectById(dicsubId);
				if (!StringUtils.isEmpty(dicsubEntity)){
					sdsStandMatchLogEntity.setDicDesc(dicsubEntity.getDicDesc());
				}
			}

			//操作内容
			if ("医嘱对照".equals(matchType) && !StringUtils.isEmpty(ordmastId)){
				CdcOrdmastEntity ordmastEntity = ordmastMapper.selectById(ordmastId);
				if (!StringUtils.isEmpty(ordmastEntity)){
					sdsStandMatchLogEntity.setOperContent(sdsStandMatchLogEntity.getDicDesc() + operTypeDesc + "医嘱表" + ordmastEntity.getDescription());
				}
			}
			if ("检查对照".equals(matchType)){
				if (!StringUtils.isEmpty(dicId)){
					CdcOrdmastEntity ordmastEntity = ordmastMapper.selectById(ordmastId);
					if (!StringUtils.isEmpty(ordmastEntity)){
						sdsStandMatchLogEntity.setOperContent(sdsStandMatchLogEntity.getDicDesc() + operTypeDesc + "医嘱表" + ordmastEntity.getDescription());
					}
				}else {
					CdcExamdicEntity cdcExamdicEntity = cdcExamdicMapper.selectById(examdicId);
					if (!StringUtils.isEmpty(cdcExamdicEntity)){
						sdsStandMatchLogEntity.setOperContent(sdsStandMatchLogEntity.getDicDesc() + operTypeDesc + "检查项目字典表" + cdcExamdicEntity.getDescription());
					}
				}
			}
			if ("检验对照".equals(matchType)){
				if (!StringUtils.isEmpty(dicId)){
					CdcLabOrderEntity cdcLabOrderEntity = cdcLabOrderMapper.selectById(laborderId);
					if (!StringUtils.isEmpty(cdcLabOrderEntity)){
						sdsStandMatchLogEntity.setOperContent(sdsStandMatchLogEntity.getDicDesc() + operTypeDesc + "检验医嘱表" + cdcLabOrderEntity.getDescription());
					}
				}else {
					CdcLabItemEntity cdcLabItemEntity = cdcLabitemMapper.selectById(labitemId);
					if (!StringUtils.isEmpty(cdcLabItemEntity)){
						sdsStandMatchLogEntity.setOperContent(sdsStandMatchLogEntity.getDicDesc() + operTypeDesc + "检验项目表" + cdcLabItemEntity.getDescription());
					}
				}
			}
		}

		//查询过滤
		List<SdsStandMatchLogEntity> res = new ArrayList<>();
		String dicDesc = (String) map.get("dicDesc");
		if (!StringUtils.isEmpty(dicDesc)){
			res = page1.stream().filter(i -> i.getDicDesc().contains(dicDesc)).collect(Collectors.toList());
			return res;
		}else {
			return page1;
		}

	}
	@Override
	public List<SdsStandMatchLogEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

}